/*
 * k5nCal - Java Swing Desktop Calendar App
 * Copyright (C) 2005-2007 Craig Knudsen, craig@k5n.us
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package trunk.view;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;

import trunk.control.UsuarioControlador;
import trunk.model.BancoDeClientes;
import trunk.model.BancoDeConvidados;
import trunk.model.calendar.Date;
import trunk.model.calendar.DisplayDate;
import trunk.model.calendar.EventInstance;

@SuppressWarnings("serial")
public class EventViewPanel extends JPanel {
	private JPanel eventDetailPanel, blankPanel;
	private CardLayout cardLayout;
	private JLabel title;
	private JLabel owner;
	private JLabel date;
	private JLabel location;
	private JLabel status;
	private JTextArea text;
	private JLabel attachment;
	private JLabel invitedsLabel;
	private BancoDeConvidados inviteds;
	private JButton confirmarButton;
	private JTable invitedsTable;
	private JScrollPane tabelaScroll;
	private TableModelConvidados clienteModel;
	private static Cursor handCursor = null, defaultCursor = null;
	Vector<JComponent> textComponents;
	private UsuarioControlador controlador;

	public EventViewPanel(UsuarioControlador controlador) {
		super ();

		this.controlador = controlador;
		this.textComponents = new Vector<JComponent> ();
		Font font = getFont ();
		font = new Font ( font.getFamily (), Font.PLAIN, font.getSize () - 2 );

		if ( EventViewPanel.defaultCursor == null ) {
			defaultCursor = this.getCursor ();
			EventViewPanel.handCursor = Cursor
			.getPredefinedCursor ( Cursor.HAND_CURSOR );
		}
		setLayout ( this.cardLayout = new CardLayout () );
		this.eventDetailPanel = new JPanel ( new BorderLayout () );

		JPanel topPanel = new JPanel ();
		topPanel.setLayout ( new GridLayout ( 7, 1 ) );
		topPanel.setBorder ( BorderFactory.createEmptyBorder ( 2, 4, 2, 4 ) );

		JPanel subpanel = new JPanel ();
		subpanel.setLayout ( new BorderLayout () );
		subpanel.add ( makeLabel ( "Título :"), BorderLayout.WEST);
		title = makeLabel( "" );
		this.textComponents.addElement(title);
		subpanel.add (title, BorderLayout.CENTER);
		topPanel.add (subpanel);

		subpanel = new JPanel ();
		subpanel.setLayout ( new BorderLayout () );
		subpanel.add ( makeLabel ( "Proprietário" + ":" ), BorderLayout.WEST );
		owner = makeLabel ( "" );
		this.textComponents.addElement ( owner );
		subpanel.add ( owner, BorderLayout.CENTER );
		topPanel.add ( subpanel );

		subpanel = new JPanel ();
		subpanel.setLayout ( new BorderLayout () );
		subpanel.add ( makeLabel ( "Data" + ":" ), BorderLayout.WEST );
		date = makeLabel ( "" );
		this.textComponents.addElement ( date );
		subpanel.add ( date, BorderLayout.CENTER );
		topPanel.add ( subpanel );

		subpanel = new JPanel ();
		subpanel.setLayout ( new BorderLayout () );
		subpanel.add ( makeLabel ( "Local" + ":" ), BorderLayout.WEST );
		location = makeLabel ( "" );
		this.textComponents.addElement ( location );
		subpanel.add ( location, BorderLayout.CENTER );
		topPanel.add ( subpanel );

		subpanel = new JPanel ();
		subpanel.setLayout ( new BorderLayout () );
		subpanel.add ( makeLabel ( "Status" + ":" ), BorderLayout.WEST );
		status = makeLabel ( "" );
		this.textComponents.addElement ( status );
		subpanel.add ( status, BorderLayout.CENTER );
		topPanel.add ( subpanel );

		subpanel = new JPanel ();
		subpanel.setLayout ( new BorderLayout () );
		subpanel.add ( makeLabel ( "Anexos" + ":" ), BorderLayout.WEST );
		attachment = makeLabel ( "" );
		this.textComponents.addElement ( attachment );
		subpanel.add ( attachment, BorderLayout.CENTER );
		topPanel.add ( subpanel );

		subpanel = new JPanel ();
		subpanel.setLayout ( new BorderLayout () );
		subpanel.add ( makeLabel ( "Convidados" + ":" ), BorderLayout.WEST );
		invitedsLabel = makeLabel ( "" );
		this.textComponents.addElement ( invitedsLabel );
		subpanel.add ( invitedsLabel, BorderLayout.CENTER );
		topPanel.add ( subpanel );

		eventDetailPanel.add ( topPanel, BorderLayout.NORTH );

		inviteds = new BancoDeConvidados();
		clienteModel = new TableModelConvidados(inviteds);
		invitedsTable = new JTable();
		invitedsTable.setAutoCreateRowSorter(true);
		invitedsTable.setModel(clienteModel);
		tabelaScroll = new JScrollPane(invitedsTable);
		tabelaScroll.setPreferredSize(new Dimension(150, 100));

		confirmarButton = new JButton("Confirmar");
		confirmarButton.setEnabled(false);
		confirmarButton.addActionListener(controlador);

		subpanel = new JPanel ();
		subpanel.setLayout ( new BorderLayout () );
		subpanel.add(tabelaScroll, BorderLayout.NORTH);
		subpanel.add(confirmarButton, BorderLayout.CENTER);

		eventDetailPanel.add (subpanel, BorderLayout.CENTER);



		text = new JTextArea ();
		text.setLineWrap ( true );
		text.setWrapStyleWord ( true );
		text.setEditable ( false );
		text.setPreferredSize(new Dimension(150, 100));
		this.textComponents.addElement ( text );
		JScrollPane scrollPane = new JScrollPane ( text );

		eventDetailPanel.add ( scrollPane, BorderLayout.SOUTH );

		this.blankPanel = new JPanel ( new BorderLayout () );
		this.blankPanel.add ( new JLabel (
				"<html>Clique num evento<br>para visualiza-lo</html>", JLabel.CENTER ),
				BorderLayout.CENTER );

		JScrollPane panelScroll = new JScrollPane(eventDetailPanel);

		add ( panelScroll, "details_panel" );
		add ( this.blankPanel, "blank_panel" );

		this.cardLayout.first ( this );

		if ( font != null )
			this.setAllFonts ( font );

		clear ();
	}

	JLabel makeLabel ( String s ) {
		JLabel l = new JLabel ( s );
		this.textComponents.addElement ( l );
		return l;
	}

	public void clear () {
		this.date.setText ( "" );
		this.location.setText ( "" );
		this.text.setText ( "" );
		this.cardLayout.last ( this );
	}

	public void setAllFonts ( Font font ) {
		super.setFont ( font );
		for ( int i = 0; i < this.textComponents.size (); i++ ) {
			JComponent o = this.textComponents.elementAt ( i );
			o.setFont ( font );
		}
	}

	public void update (EventInstance event ) {
		if ( event == null ) {
			this.clear ();
			this.cardLayout.last ( this );
			return;
		}
		Date eventDate = new Date(event.getYear(), event.getMonth(),
				event.getDayOfMonth(), event.getHour(), event.getMinute(), 0);
		DisplayDate dd = new DisplayDate ( eventDate );
		this.title.setText( event.getTitle());
		this.owner.setText( event.getOwner().getNome() );
		this.date.setText ( dd.toString () );

		if ( event.getLocation () != null )
			this.location.setText ( event.getLocation () );
		else
			this.location.setText ( "" );
		
		this.status.setText( event.getStatus().toString() );

		this.attachment.setText( event.hasAttachment()?"possui":"não possui" );

		if( event.getInviteds() != null && event.getInviteds().size() > 0 )
			this.setInviteds(event.getInviteds());

		if( event.getInviteds() != null && 
				event.getInviteds().isConvidadoPendente(controlador.usuario) )
			this.confirmarButton.setEnabled(true);
		else
			this.confirmarButton.setEnabled(false);

			if ( event.getDescription () != null )
				this.text.setText ( event.getDescription () );
			else
				this.text.setText ( "" );
		this.text.setCaretPosition ( 0 );
		this.cardLayout.first ( this );
	}

	public void setInviteds(BancoDeConvidados convidados){
		this.inviteds = convidados;
		clienteModel.linhas = inviteds.getBancoDeConvidados();
		clienteModel.fireTableDataChanged();
	}

	public JButton getConfirmarButton() {
		return confirmarButton;
	}
	
	public void setConfirmarButtonEnable(Boolean enable){
		this.confirmarButton.setEnabled(enable);
	}
}
